﻿
#include <iostream>

#include "hy_robot.h"
#include "hy_robot_logic.h"

//robot config file
//static const char* robot_config = "robot_config";

hy_robot::hy_robot() 
:_robot_total(1),_ipaddr("192.168.254.167"),_port(6000),_robot_name("rfrreferf")
,_move(true),_chat(false),_arena(false),_boss(false)
{

}

hy_robot::~hy_robot()
{

}

hy_err_t hy_robot::unit()
{
	//_config.unit();
	_net_moudle.unit();
	return hy_right;
}

hy_err_t hy_robot::init()
{
	//_config.init();
	_net_moudle.init();
	char isdefault;
	std::cout<<"default config:[y/n]";
	std::cin>>isdefault;
	if (isdefault == 'y' || isdefault == 'Y')
	{
		return hy_right;
	}

	std::cout<<"ip addr:\n";
	std::cin>>_ipaddr;
	std::cout<<"ip port:\n";
	std::cin>>_port;
	std::cout<<"robot name:\n";
	std::cin>>_robot_name;
	std::cout<<"robot total:\n";
	std::cin>>_robot_total;

	std::cout<<"能否移动:[y/n]\n";
	char move;
	std::cin>>move;
	_move =  (move == 'y'||move == 'Y');
	

	std::cout<<"能否聊天:[y/n]\n";
	std::cin>>move;
	_chat = (move == 'y'||move == 'Y');

	std::cout<<"能否竞技场:[y/n]\n";
	std::cin>>move;
	_arena = (move == 'y'||move == 'Y');

	std::cout<<"能否世界boss:[y/n]\n";
	std::cin>>move;
	_boss = (move == 'y'||move == 'Y');

	return hy_right;
}

//*************************************************************
// Method:    hy_robot::run
// Returns:   void
// Qualifier:
//*************************************************************
void hy_robot::run()
{
	_net_moudle.run();

	for (int i = 0; i <_robot_total; i++ )
	{
		hy_trace("robot [%s] [%d] [%d]",_ipaddr.data(),_port,i);

		hy_robot_logic* robot = new hy_robot_logic(i,this,_robot_name,_move,_chat,_arena,_boss);
		_robots.push_back(robot);

		robot->start(_ipaddr.data(),_port);
		hy_os::hy_sleep(100);
	}

	hy_os::hy_sleep(1000);
	
	for (;;)
	{
		hy_u32_t action_count = 0;
		hy_u32_t online = 0;
		for (size_t i = 0; i < _robots.size(); i++)
		{
			_robots[i]->action(action_count++);
			if (_robots[i]->_online)
			{
				online ++;
			}
		}
		hy_log(ll_must,"同时在线机器人数量[%d]",online);
		hy_os::hy_sleep(5000);
	}
}

void hy_robot::stop()
{
	//_net_moudle.stop();
}

hy_robot_net_module* hy_robot::get_net_module()
{
	return &_net_moudle;
}

